package com.hudongwx.food.adapter;


import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hudongwx.food.R;
import com.hudongwx.food.activity.TiaoZhuanSheQuReTieActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.xutils.x;

/**
 * Author:降初
 * DateTime:16-12-14 上午9:24
 */
public class MyListViewAdapter1 extends BaseAdapter{
    Context context;
    JSONArray listview1;
    public MyListViewAdapter1(JSONArray listview1,Context context ) {
        this.listview1 = listview1;
        this.context = context;
    }


    @Override
    public int getCount() {
        return listview1.length();
    }

    @Override
    public Object getItem(int position) {
        return getItem(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        MyList1ViewHolder listHolder;
        if (null==convertView){
            listHolder = new MyList1ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.home_blogs_1,null);
            listHolder.tv1  = (TextView) convertView.findViewById(R.id.tv_blogs);
            listHolder.tv2  = (TextView) convertView.findViewById(R.id.tv_blogs1);
            listHolder.iv = (ImageView) convertView.findViewById(R.id.image_blogs);
            convertView.setTag(listHolder);
        }else {
            listHolder = (MyList1ViewHolder) convertView.getTag();
        }

        try {
            listHolder.tv1.setText(listview1.getJSONObject(position).getString("Title"));
            x.image().bind(listHolder.iv,listview1.getJSONObject(position).getString("CoverImg"));
            listHolder.tv2.setText(listview1.getJSONObject(position).getString("BoardName"));
            listHolder.iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, TiaoZhuanSheQuReTieActivity.class);
                    int i = 0;
                    try {
                        i = listview1.getJSONObject(position).getInt("Id");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    intent.putExtra("type",i);
                    context.startActivity(intent);
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return convertView;
    }

    class MyList1ViewHolder{
        TextView tv1,tv2;
        ImageView iv;
    }
}
